package com.appcenter.application.service;

import com.appcenter.application.dto.roleusers.RoleUserDto;
import com.appcenter.application.expand.ObjectFinal;
import com.appcenter.application.input.roleuser.*;
import com.appcenter.application.mapper.AppsMapper;
import com.appcenter.application.mapper.RoleUsersMapper;
import com.appcenter.application.mapper.RolesMapper;
import com.appcenter.application.mapper.UsersMapper;
import com.appcenter.application.model.RoleUsers;
import com.appcenter.application.model.Roles;
import com.appcenter.application.model.Users;
import com.appcenter.application.output.roleuser.*;
import com.appcenter.jz.Logger.BusinessLogger;
import com.appcenter.jz.Logger.LoggerGenre;
import com.appcenter.jz.app.CurrentUser;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.page.PagedResult;
import com.appcenter.jz.unit.ObjectUnit;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class RoleUserService {
    @Autowired
    RoleUsersMapper roleUsersMapper;
    @Autowired
    UsersMapper usersMapper;

    @Autowired
    AppsMapper appsMapper;
    @Autowired
    RolesMapper rolesMapper;

    @Autowired
    ObjectFinal objectFinal;

    /**
     *  创建人:Coke
     *  备注: 创建角色人员信息
     *  时间: 2019/4/26 14:05 
     *  参数: 
     */
    public CreateRoleUserOutput CreateRoleUser(CreateRoleUserInput input) throws Exception {
        CreateRoleUserOutput output = new CreateRoleUserOutput();

        Users user = usersMapper.getUsersByUserCode(input.getItem().getUserCode(),objectFinal.getDataEffectiveVersion());
        if (user == null) {
            throw new ErrorJsonResult("1000071", "用户不存在！");
        }
        Example example = new Example(RoleUsers.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId", input.getItem().getRoleId());
        criteria.andEqualTo("userId", user.getId());
        criteria.andEqualTo("validStatus", 1);
        List<RoleUsers> countries = roleUsersMapper.selectByExample(example);

        int RoleUserCount = countries.size();
        if (RoleUserCount > 0) {
            throw new ErrorJsonResult("1000071", "该角色下用户已存在！");
        }
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        RoleUsers curRoleUser= new RoleUsers();
        curRoleUser.setId(uuid);
        curRoleUser.setRoleId(input.getItem().getRoleId());
        curRoleUser.setUserId(user.getId());

        curRoleUser.setValidStatus(true);
        curRoleUser.setCreateTime(new Date());
        curRoleUser.setCreateUserId(CurrentUser.getUserId());
        roleUsersMapper.insertSelective(curRoleUser);
        BusinessLogger.Info(LoggerGenre.Add, "添加角色对应人员信息", input);

        RoleUserDto item = new RoleUserDto();
        BeanUtils.copyProperties(curRoleUser,item);
        output.setItem(item);
        return output;
    }

    /**
     *  创建人:Coke
     *  备注: 删除角色人员信息
     *  时间: 2019/4/26 14:06 
     *  参数: 
     */
    public DeleteRoleUserOutput DeleteRoleUser(DeleteRoleUserInput input) throws Exception {
        DeleteRoleUserOutput output = new DeleteRoleUserOutput();

        RoleUsers oldRoleUser = roleUsersMapper.selectByPrimaryKey(input.getItem().getId());
        oldRoleUser.setValidStatus(false);
        roleUsersMapper.updateByPrimaryKey(oldRoleUser);
        BusinessLogger.Info(LoggerGenre.Delete, "删除角色对应人员信息", input);
        return output;

    }

    /**
     *  创建人:Coke
     *  备注: 获取角色人员信息
     *  时间: 2019/4/26 14:06 
     *  参数: 
     */
    public GetRoleUsersOutput GetRoleUsers(GetRoleUsersInput input) throws Exception {
        GetRoleUsersOutput output = new GetRoleUsersOutput();
        input.setVersionNo(objectFinal.getDataEffectiveVersion());
        List<String> appIds= objectFinal.GetPermissionAppIds(ObjectFinal.APP_CODE,CurrentUser.getUserCode());
        input.setAppIds(appIds);

        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"id desc");
        List<RoleUserDto> ls = roleUsersMapper.searchRoleUser(input);
        PageInfo<RoleUserDto> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<RoleUserDto>(page.getList(),(int)page.getTotal()));
        BusinessLogger.Info(LoggerGenre.View, "获取角色对应人员信息", input);
        return output;
    }

    /**
     *  创建人:Coke
     *  备注: 加载角色人员信息
     *  时间: 2019/4/26 14:06 
     *  参数: 
     */
    public PageLoadOutput PageLoad(PageLoadInput input) throws Exception {
        PageLoadOutput output = new PageLoadOutput();
        output.setItems(GetRoleUsers(input).getItems());
        output.setApps(objectFinal.GetPermissionApps(ObjectFinal.APP_CODE,CurrentUser.getUserCode()));

        Example exampleRole = new Example(Roles.class);
        Example.Criteria criteriaRole = exampleRole.createCriteria();
        criteriaRole.andEqualTo("validStatus", 1);
        output.setRoles(rolesMapper.selectByExample(exampleRole));
        return output;
    }
    //---------RoleUserService-------------
    /**
     *  创建人:Coke
     *  备注: 获取角色人员信息
     *  时间: 2019/4/26 14:25
     *  参数: 
     */
    public GetRoleUsersServiceOutput GetRoleUsers(GetRoleUsersServiceInput input) {
        GetRoleUsersServiceOutput output = new GetRoleUsersServiceOutput();
        List<RoleUserDto> ls=roleUsersMapper.getRoleUsers(input);
        output.setItems(new PagedResult<RoleUserDto>(ls,ls.size()));
        return output;
    }

}
